'use strict';

const Controller = require('egg').Controller;

// 时间戳转化
const timestampToTime = timestamp => {
  const date = new Date(timestamp);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
};

const time = timestampToTime(new Date())

// time: timestampToTime(new Date()),
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  // **************** 房源管理 ********************
  // 获取房源管理数据
  async getHouseList() {
    const { ctx, app } = this;
    const { type } = ctx.request.body;

    if (type === 0) {
      const data = await app.mysql.query(`SELECT * FROM houselist WHERE state = ''`)
      ctx.body = {
        code: 1,
        data,
      };
    } else {
      const data = await app.mysql.query(`SELECT * FROM houselist WHERE state = '正常'`)
      ctx.body = {
        code: 1,
        data,
      };
    }
  }

  //  房源管理  删除事件
  async apiDelHouseList() {
    const { ctx, app } = this;

    const { id, state } = ctx.request.body;

    await app.mysql.delete('houselist', { id })
    const data = await app.mysql.query(`SELECT * FROM houselist WHERE state = '${state}'`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // // 更改switch事件
  async upSwitchState() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.update('houselist', { id, state: '' })

    const data = await app.mysql.query(`SELECT * FROM houselist WHERE state = '正常'`)

    ctx.body = {
      code: 1,
      data,
      msg: '更改成功',
    };
  }

  // 房源管理 搜索事件
  async apiSearchHouse() {
    const { ctx, app } = this;

    const { province = '', district = '', county = '', state = '' } = ctx.request.body;

    const data = await app.mysql.query(`SELECT * FROM houselist WHERE address like'%${province + district + county}%' and state like '%${state}%'`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 房源管理 编辑事件 加  新增
  async apiEditHouse() {
    const { ctx, app } = this;

    const { values, id = '', broker = '', img = '', video = '', state = '', coordinates = '' } = ctx.request.body;

    // console.log(values);

    const { area = '', name = '', price = '', rent = '', address = '', tags = '', featured = '', introduction = '',
      booking = '', developers = '', envelopes = '', discounts = '', situation = '', salesStatus = '',
      types = '', checking = '', cell = '',
      buildingtypes = '', property = '', propertys = '', structure = '', ratio = '', greening = '', number = '',
      situations = '', spaceType = '', company = '', propertyfee = '' } = values

    if (id !== '') {
      await app.mysql.update('houselist', {
        id, sort: 1, time, state, broker, cell,
        area: area.toString(), name, price, rent, address, tags: tags.toString(), featured: featured.toString(), introduction,
        booking, developers, coordinates: coordinates.toString(), envelopes, discounts, situation, salesStatus,
        types: types.toString(), img, video, checking,
        buildingtypes, property, propertys, structure, ratio, greening, number,
        situations, spaceType, company, propertyfee
      })

      ctx.body = {
        code: 1,
        msg: '更改成功',
      };

    } else {
      await app.mysql.insert('houselist', {
        area: area.toString(), name, address, price, rent, sort: 1, time, state: '', broker: '', cell,
        booking, developers, coordinates: coordinates.toString(), envelopes, discounts, situation, salesStatus,
        types: types.toString(), tags: tags.toString(), featured: featured.toString(), img,
        video, checking, introduction,
        buildingtypes, property, propertys, structure, ratio, greening, number,
        situations, spaceType, company, propertyfee
      })
      ctx.body = {
        code: 1,
        msg: '添加成功',
      };
    }

  }


  // *****************资讯管理*******************
  // 获取资讯管理数据
  async getInformList() {
    const { ctx, app } = this;

    const data = await app.mysql.query(`SELECT * FROM informlist`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 资讯管理 删除事件
  async apiDelInformList() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.delete('informlist', { id })
    const data = await app.mysql.query(`SELECT * FROM informlist`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 资讯管理 switch 更新
  async upInformList() {
    const { ctx, app } = this;

    const { id, state } = ctx.request.body;

    if (state === '开启') {
      await app.mysql.update('informlist', { id, state: '关闭' })
      const data = await app.mysql.query(`SELECT * FROM informlist`)
      ctx.body = {
        code: 1,
        data,
        msg: '更改成功'
      };
    } else {
      await app.mysql.update('informlist', { id, state: '开启' })
      const data = await app.mysql.query(`SELECT * FROM informlist`)
      ctx.body = {
        code: 1,
        data,
        msg: '更改成功'
      };
    }
  }

  // 资讯管理 搜索事件
  async apiSearchInform() {
    const { ctx, app } = this;

    const { title = '', state = '' } = ctx.request.body;

    const data = await app.mysql.query(`SELECT * FROM informlist WHERE state like '%${state}%' and title like '%${title}%'`)

    ctx.body = {
      code: 1,
      data,
      msg: '查询成功'
    };
  }

  // 资讯管理 编辑 加 新建
  async apiAddEditInform() {
    const { ctx, app } = this;

    const { title, name, id = '' } = ctx.request.body;

    if (id == '') {
      await app.mysql.insert('informlist', {
        title,
        name,
        time,
        state: '开启'
      })

      const data = await app.mysql.query(`SELECT * FROM informlist`)

      ctx.body = {
        code: 1,
        msg: "添加成功",
        data,
      };

    } else {

      await app.mysql.update('informlist', { id, title, name, time })

      const data = await app.mysql.query(`SELECT * FROM informlist`)

      ctx.body = {
        code: 1,
        msg: '更改成功',
        data
      };
    }

  }


  // ************** 经纪人管理 *************、
  //  经纪人管理 获取数据
  async apiGetBroker() {
    const { ctx, app } = this;

    let data = await app.mysql.query(`SELECT * FROM broker`)

    data = data.sort((a, b) => {
      return b.id - a.id
    })
    ctx.body = {
      code: 1,
      data,
    };
  }

  // 经纪人管理 查询 
  async apiSearchBroker() {
    const { ctx, app } = this;

    const { cell = '', state = '' } = ctx.request.body;

    const data = await app.mysql.query(`SELECT * FROM broker WHERE cell like '%${cell}%' and state like '%${state}%'`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 经纪人管理 删除事件
  async apiDelBroker() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.delete('broker', { id })
    const data = await app.mysql.query(`SELECT * FROM broker`)

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 经纪人管理 启用 
  async apiStartBroker() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.update('broker', { id, state: '在职' })

    ctx.body = {
      code: 1,
      msg: '启用成功',
    };
  }

  // 经纪人管理 禁用
  async apiStopBroker() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.update('broker', { id, state: '离职' })

    ctx.body = {
      code: 1,
      msg: '禁用成功',
    };
  }

  // 经纪人管理 编辑 加 新建
  async apiAddEditBroker() {
    const { ctx, app } = this;

    const { name, cell, id = '', company, state } = ctx.request.body

    // console.log(name, cell, id.company, state);
    if (id == '') {
      await app.mysql.insert('broker', {
        name,
        cell,
        company,
        bridal: '100',
        second: '100',
        renting: '100',
        state,
      })

      let data = await app.mysql.query(`SELECT * FROM broker`)
      data = data.sort((a, b) => {
        return b.id - a.id
      })

      ctx.body = {
        code: 1,
        msg: "添加成功",
        data,
      };

    } else {

      await app.mysql.update('broker', { id, cell, name, company, state })

      let data = await app.mysql.query(`SELECT * FROM broker`)

      data = data.sort((a, b) => {
        return b.id - a.id
      })

      ctx.body = {
        code: 1,
        msg: '更改成功',
        data
      };
    }
  }


}

module.exports = HomeController;
